package com.xinlong.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.xinlong.entity.Permission;
import com.xinlong.entity.Role;
import com.xinlong.service.IroleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class Rolecontroller {


    @Autowired
    IroleService iroleService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        ModelAndView mv=new ModelAndView();
        List<Role> findall = iroleService.findall();
        mv.addObject("roleList",findall);
        mv.setViewName("role-list");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(Role role){
        iroleService.save(role);
        return "redirect:findAll.do";
    }

    @RequestMapping("/findRoleByIdAndAllPermission.do")
    public ModelAndView findRoleByIdAndAllPermission(String id){
        ModelAndView mv=new ModelAndView();
        Role findbyid = iroleService.findbyid(id);
        List<Permission> roleByIdAndAllPermission = iroleService.findRoleByIdAndAllPermission2();
        mv.addObject("role",findbyid);
        mv.addObject("Permissionlist",roleByIdAndAllPermission);
        mv.setViewName("role-permission-add");
        return mv;
    }

    @RequestMapping("/addRoleTopermissin.do")
    public String addRoleTopermissin(String roleid,String[] ids){
        iroleService.delRoleTopermissin(Integer.parseInt(roleid));
        iroleService.addRoleTopermissin(roleid,ids);

        return "redirect:findAll.do";
    }

}
